package com.lianniu.mgr.baseService.menu.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.menu.model.OperationManageVo;
import com.lianniu.mgr.baseService.menu.service.IOperationManageService;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
/**
 * 

 * @date 2016年3月28日
 */
@Controller
@RequestMapping("/rest")
public class OperationManageRestController {
	  private final static Logger logger = Logger.getLogger(OperationManageRestController.class);
	  @Autowired
	  IOperationManageService operationManageService;
	  
	  /**
	   * 
	  * @Title: operationList 
	  * @Description: 操作列表展示
	  * @return BaseResult    返回类型
	   */
	  @RequestMapping(value="/operationList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult operationList(HttpServletRequest request,OperationManageVo vo,PaginationInfo paginationInf) {
		    logger.info("OperationManageRestController operationList begin.");
		    try {
		    	String menuId = vo.getMenuId();
		      logger.info("getOperationList begin.");
		      BaseResult baseResult = operationManageService.getOperByMenuIdForPage(vo, paginationInf);
		      logger.info("getOperationList end.");
		      logger.info("OperationManageRestController operationList end.");
		      return baseResult;
		    } catch (Exception e) {
		      logger.error("OperationManageRestController operationList error:" + e.getMessage());
		      logger.error("OperationManageRestController operationList end.");
		      return BaseResult.exception(e.getMessage());
		    }
		  }
	  /**
	   * 
	  * @Title: delOperation 
	  * @Description: 按照id删除操作项
	  * @return BaseResult    返回类型
	   */
	  @RequestMapping(value="/delOperation",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult delOperation(HttpServletRequest req,String chk){
		  logger.info("OperationManageRestController delOperation begin.");
		  try {
			logger.info("delOperation begin.");
			String[] idArr = chk.split("-");
			List<String> list = new ArrayList<String>();
			for (String  s : idArr) {
				list.add(s);
			}
			BaseResult baseResult = operationManageService.delOperation(list);
			logger.info("delOperation end.");
		    logger.info("OperationManageRestController delOperation end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("OperationManageRestController delOperation error:" + e.getMessage());
		    logger.error("OperationManageRestController delOperation end.");
		    return BaseResult.exception(e.getMessage());
		}
	  }
	  /**
	   * 
	  * @Title: toOperationAdd 
	  * @Description: 编辑页查询操作
	  * @return BaseResult    返回类型
	   */
	  @RequestMapping(value="/toOperationAdd",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult toOperationAdd(HttpServletRequest req,OperationManageVo vo){
		    logger.info("OperationManageRestController toOperationAdd begin.");
		    try {
				logger.info("toOperationAdd begin.");
				if(!StringUtil.isEmpty(vo.getId())){
					BaseResult baseResult = operationManageService.getOperById(vo);
					logger.info("toOperationAdd end.");
				    logger.info("OperationManageRestController toOperationAdd end.");
					return baseResult;
				}
				logger.info("toOperationAdd end.");
			    logger.info("OperationManageRestController toOperationAdd end.");
				return new BaseResult(null, null, null);
			} catch (Exception e) {
				logger.error("OperationManageRestController toOperationAdd error:" + e.getMessage());
			    logger.error("OperationManageRestController toOperationAdd end.");
				return BaseResult.exception(e.getMessage());
			}
		    
	  }
	  /**
	   * 
	  * @Title: operationAdd 
	  * @Description: 新增或更新操作
	  * @return BaseResult    返回类型
	   */
	  @RequestMapping(value="/operationAdd",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult operationAdd(HttpServletRequest req,OperationManageVo vo){
		  logger.info("OperationManageRestController operationAdd begin.");
		  try {
			logger.info("operationAdd begin.");
			if(!StringUtil.isEmpty(vo.getId())){
				BaseResult baseResult = operationManageService.editOperation(vo);
				logger.info("operationAdd end.");
			    logger.info("OperationManageRestController operationAdd end.");
				return  baseResult;
			}else{
				String uuid = StringUtil.getUUID();
				vo.setId(uuid);
				BaseResult baseResult = operationManageService.addOperation(vo);
				logger.info("operationAdd end.");
			    logger.info("OperationManageRestController operationAdd end.");
				return baseResult;
			}
		} catch (Exception e) {
			logger.error("OperationManageRestController operationAdd error:" + e.getMessage());
		    logger.error("OperationManageRestController operationAdd end.");
			return BaseResult.exception(e.getMessage());
		}
	  }

	  
}
